import {Injectable} from "@angular/core";
import {RequestServerHTTPService} from "../../../shared/service/request-server-HTTP.service";
import {UserModel} from "../../../shared/model/user.model";
/**
 *  This class is used to request remote server for user register.
 */
@Injectable()
export class UserRegisterHttpService {
    private checkUsernameURL: string = "user/checkUsername.do";
    private registerURL: string = "user/register.do";

    constructor(private requestServerHttpService: RequestServerHTTPService) {
    }

    /**
     * This method is used to check the target username is existing or not.
     * @param usernameValidator
     * @param callback
     */
    public checkUsernameInRemote(usernameValidator: string, callback: any): void {
        this.requestServerHttpService.getRequestServerForBack(this.checkUsernameURL, {
            username: usernameValidator
        }, result => callback(result), er => UserRegisterHttpService.errorFn(er));
    }

    /**
     * this method is used to register user in server.
     * @param user
     * @param callback
     */
    public register(user: UserModel, callback: any) {
        this.requestServerHttpService.getRequestServerForBack(this.registerURL, user,
            result => callback(result), er => UserRegisterHttpService.errorFn);
    }

    private static errorFn(error) {
        alert(error);
    }
}